package com.cc.springboot_test.listener;

import com.cc.springboot_test.Application;
import com.cc.springboot_test.monitor.LogMonitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.sql.DriverManager;
import java.sql.SQLException;

public class LogMonitorListener implements ServletContextListener {
    private Logger logger = LoggerFactory.getLogger(LogMonitorListener.class);

    @Override
    public void contextInitialized(ServletContextEvent sce) {

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        try {
            DriverManager.deregisterDriver(DriverManager.getDrivers().nextElement());
            LogMonitor monitor = Application.getBean("logMonitor", LogMonitor.class);
            if (monitor != null) {
                monitor.shutdown();
            }
        } catch (SQLException e) {
            logger.error("monitor listener error: ", e);
        }
    }
}
